package demo10;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//1.继承 Thread 类，重写 run 方法
class myThread extends Thread {

    public volatile Object lock = new Object();
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}
public class Solution {


    public static void main(String[] args) {
        Thread t1 = new myThread();
        t1.start();
        Thread t2 = new Thread(new myRunnable());
        t2.start();

        //3.
        Thread thread = new Thread() {
            @Override
            public void run() {
                synchronized(this) {
                    System.out.println("使用匿名内部类，实现 Thread 子类对象");
                }
            }
        };

        //4.
        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("使用匿名内部类，实现 Runnable 接口，重写 run 方法!");
            }
        });

        //5.
        Thread t5 = new Thread(()-> {
            System.out.println("使用 lambde 表达式");
        });

        //6.
        ExecutorService pool = Executors.newCachedThreadPool();
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("使用线程池!");
            }
        });

    }

}
//2. 实现 Runnable 接口，重写 run 方法
class myRunnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(i);
        }
    }
}